package udp_client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class mySend implements Runnable{
	private DatagramSocket ds;
	private String serverHost;
	private int serverPort;
	private static BlockingQueue<byte[]> pkgQ= new LinkedBlockingQueue<byte[]>();
	public mySend(){}
	public mySend(DatagramSocket ds, String serverHost, int serverPort){
		this.ds= ds;
		this.serverHost= serverHost;
		this.serverPort= serverPort;
	}
	public void run() {
		int index=0;
		while(true){
			while(pkgQ.isEmpty()==false){
//				System.out.println("mySend::"+pkgQ.size());
				byte[] SingePkg= pkgQ.peek();
//				System.out.println("mySend::"+Arrays.toString(SingePkg));
//				System.out.println("mySend::"+SingePkg.length);
				pkgQ.remove();
				DatagramPacket dp=null;
				try {
					dp = new DatagramPacket(SingePkg, SingePkg.length, InetAddress.getByName(this.serverHost), this.serverPort);
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
//					ds.send(dp);
					
//					if (index>70 && index<120) {
						ds.send(dp);
//						System.out.println("client finish one send "+index);
//					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				index++;
			}
		} 
	}
	public Queue<byte[]> get_pkgQ(){
		return pkgQ;
	}
}
